package com.kaili.finance.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.finance.pojo.dto.account.FinAccountQueryDTO;
import com.kaili.finance.pojo.dto.detailSum.FinInOutDetailDTO;
import com.kaili.finance.pojo.dto.detailSum.finInOutQueryDTO;
import com.kaili.finance.pojo.dto.transSum.FinTransSumDetailQueryDTO;
import com.kaili.finance.pojo.dto.transSum.FinTransSumQueryDTO;
import com.kaili.finance.pojo.vo.*;
import com.kaili.finance.service.AccountService;
import com.kaili.finance.service.FinDetailService;
import com.kaili.finance.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by :xinglei
 * *  *  *  *  *  *  *  *  *
 *
 * @Created time :18-5-17.
 * @Company :杭州凯立通信有限公司
 * *  *  *  *  *  *  *  *  *
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "交易明细", description = "交易明细相关API")
public class FinDetailSumController extends BaseController{

    @Autowired
    private FinDetailService finDetailService;
    @Autowired
    private AccountService accountService;

    @GetMapping("/finDetilSum")
    @ApiOperation(value = "查询收入支出汇总数据", notes = "查询收入支出汇总的数据API")
    public Page<FinInOutVO> listFinDetilSum(@ModelAttribute finInOutQueryDTO dto) throws Exception {
        //结束时间只允许截至昨日 23：59：59
        checkCreateTime2(dto,dto.getCreatedEndTime());

        Page<FinInOutVO> pager = new Page<FinInOutVO>(dto.getPage(), dto.getPageSize());
        return finDetailService.selectFinDetailPage(pager, dto);
    }

    @GetMapping("/finDetilSumAmount")
    @ApiOperation(value = "查询收入支出汇总数据金额", notes = "查询收入支出汇总数据金额API")
    public FinTransSumAmountVO listFinDetilSumAmount(@ModelAttribute finInOutQueryDTO dto) throws Exception {
        //结束时间只允许截至昨日 23：59：59
        checkCreateTime2(dto,dto.getCreatedEndTime());

        return finDetailService.selectFinDetailSumAmount(dto);
    }

    @GetMapping("/finDetil")
    @ApiOperation(value = "查询收入支出数据明细", notes = "查询收入支出汇总的明细数据API")
    public Page<FinInOutDetailVO> listFinDetil(@ModelAttribute FinInOutDetailDTO dto) throws Exception {
        Page<FinInOutDetailVO> pager = new Page<FinInOutDetailVO>(dto.getPage(), dto.getPageSize());
        return finDetailService.selectFinDetail(pager, dto);
    }

    @GetMapping("/finSite")
    @ApiOperation(value = "根据id或网点名成模糊查询网点", notes = "根据id或网点名成模糊查询网点API")
    public List<FinSiteVO> queryLike(@ModelAttribute FinAccountQueryDTO dto){
        List<FinSiteVO> list=null;
        try{
            Long.parseLong(dto.getSiteName());
            list=accountService.querySiteNameByLike("%"+dto.getSiteName()+"%",null);
        }catch (NumberFormatException e){
            list=accountService.querySiteNameByLike(null,"%"+dto.getSiteName()+"%");
        }
        return list;
    }

    @GetMapping("/transSum")
    @ApiOperation(value = "交易汇总查询", notes = "交易汇总查询的数据API")
    public Page<FinDetailDayVO> listTransSum(@ModelAttribute FinTransSumQueryDTO dto) throws Exception {
        //结束时间只允许截至昨日 23：59：59
        checkCreateTime(dto,dto.getCreatedEndTime());
        //判断是否为总部登陆
        if(LoginDetailUtils.getSiteId()!=1){
            dto.setSiteId(LoginDetailUtils.getSiteId().longValue());
        }
        Page<FinDetailDayVO> pager = new Page<FinDetailDayVO>(dto.getPage(), dto.getPageSize());
        return finDetailService.selectFinTransSum(pager, dto);
    }

    @GetMapping("/transSumAmount")
    @ApiOperation(value = "交易汇总金额查询", notes = "交易汇总金额查询API")
    public FinTransSumAmountVO listTransSumAmount(@ModelAttribute FinTransSumQueryDTO dto) throws Exception {
        //结束时间只允许截至昨日 23：59：59
        checkCreateTime(dto,dto.getCreatedEndTime());
        //判断是否为总部登陆
        if(LoginDetailUtils.getSiteId()!=1){
            dto.setSiteId(LoginDetailUtils.getSiteId().longValue());
        }
        return finDetailService.selectFinTransSumAmount(dto);
    }


    @GetMapping("/chargeItems")
    @ApiOperation(value = "查询所有费用项目", notes = "查询所有费用项目API")
    public List<FinDictVO> selectChargeItem(){

        List<FinDictVO> list=finDetailService.selectAllChargeItem();
        return list;
    }

    @GetMapping("/transSumDetail")
    @ApiOperation(value = "交易汇总明细查询", notes = "交易汇总明细查询API")
    public Page<FinTransSumDetailVO> listTransSumDetail(@ModelAttribute FinTransSumDetailQueryDTO dto) throws Exception {

        Page<FinTransSumDetailVO> pager = new Page<FinTransSumDetailVO>(dto.getPage(), dto.getPageSize());
        return finDetailService.selectFinTransDeatil(pager, dto);
    }

}
